package com.navi.mould1.service.impl;

import com.navi.bean.in.ConditionBean;
import com.navi.bean.out.productClassify_report.ProductClassifyReportGridOut;
import com.navi.bean.sql.SqlVo;
import com.navi.mould1.service.IProductClassifyReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
import static com.navi.bean.sql.SqlVo.jointInSql;
import static com.navi.core.util.JdbcUtils.mySql_QueryRunner_QueryListBySql;
import static com.navi.core.util.JdbcUtils.sqlServer_QueryRunner_QueryListBySql;
import static com.navi.mould1.reportUtils.ComputeUtil.getPercent;
import static com.navi.mould1.reportUtils.ComputeUtil.getRate;


@Slf4j
@Service
public class ProductClassifyReportServiceImpl implements IProductClassifyReportService {


    public List<ProductClassifyReportGridOut> getGridData(ConditionBean conditions) {
        final SqlVo sqlVo = new SqlVo();
        sqlVo.appendSql("SELECT");
        sqlVo.appendSql("attr_1, pm_name, lot_number, bom_material_code_desc, pm, dec_ctm");
        sqlVo.appendSql("FROM dbo.iv_distribution");
        sqlVo.appendSql("WHERE 1=1");
        if (StringUtils.isNotEmpty(conditions.getBay_id())) sqlVo.appendSql("AND equipment_code in" + jointInSql(conditions.getBay_id()));
        if (StringUtils.isNotEmpty(conditions.getProduct_material())) sqlVo.appendSql("AND material_code in" + jointInSql(conditions.getProduct_material()));
        int bom_material_length = 0;
        if (StringUtils.isNotEmpty(conditions.getBom_material())) {
            sqlVo.appendSql("AND bom_material_code in" + jointInSql(conditions.getBom_material()));
            bom_material_length = conditions.getBom_material().split(",").length;
        }
        if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND test_time >= ?", conditions.getStart_time()+":00:00");
        if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND test_time < ?", conditions.getEnd_time()+":00:00");
        sqlVo.appendSql("AND lot_number in (");
            sqlVo.appendSql("SELECT");
            sqlVo.appendSql("lot_number");
            sqlVo.appendSql("FROM dbo.iv_distribution");
            sqlVo.appendSql("WHERE 1=1");
            if (StringUtils.isNotEmpty(conditions.getBay_id())) sqlVo.appendSql("AND equipment_code in" + jointInSql(conditions.getBay_id()));
            if (StringUtils.isNotEmpty(conditions.getProduct_material())) sqlVo.appendSql("AND material_code in" + jointInSql(conditions.getProduct_material()));
            if (StringUtils.isNotEmpty(conditions.getBom_material())) sqlVo.appendSql("AND bom_material_code in" + jointInSql(conditions.getBom_material()));
            if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND test_time >= ?", conditions.getStart_time()+":00:00");
            if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND test_time < ?", conditions.getEnd_time()+":00:00");
            sqlVo.appendSql("GROUP BY lot_number HAVING COUNT(DISTINCT(bom_material_code)) >= " + bom_material_length);
        sqlVo.appendSql(")");
        List<ProductClassifyReportGridOut> gridOut_list = sqlServer_QueryRunner_QueryListBySql(sqlVo, ProductClassifyReportGridOut.class);


        //Mysql-电池车间效率产出数量
        final SqlVo sqlVo2 = new SqlVo();
        sqlVo2.appendSql("SELECT");
        sqlVo2.appendSql("gear,");
        sqlVo2.appendSql("sum(total_count) as gear_count");
        sqlVo2.appendSql("FROM icim_jn.halm_test_data_summary");
        sqlVo2.appendSql("WHERE 1=1");
        //if (StringUtils.isNotEmpty(conditions.getGear())) sqlVo2.appendSql("AND A.GEAR in" + jointInSql(conditions.getGear()));
        if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo2.appendSqlAndAddParam("AND CONCAT(INPUT_DATE, ' ', INPUT_TIME, ':00:00') >= ?", conditions.getStart_time()+":00:00");
        if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo2.appendSqlAndAddParam("AND CONCAT(INPUT_DATE, ' ', INPUT_TIME, ':00:00') < ?", conditions.getEnd_time()+":00:00");
        sqlVo2.appendSql("GROUP BY gear");
        List<ProductClassifyReportGridOut> gear_count_list = mySql_QueryRunner_QueryListBySql(sqlVo2, ProductClassifyReportGridOut.class);


        gridOut_list.stream().filter(data -> StringUtils.isNotEmpty(data.getAttr_1())).map(data -> {
            long gear_count = gear_count_list.stream().filter(gear_data -> {
                String attr_1 = data.getAttr_1().trim().replace("%", "");
                while (attr_1.endsWith("0")) {
                    attr_1 = attr_1.endsWith("0") ? attr_1.substring(0, attr_1.length() - 1) : attr_1;
                    attr_1 = attr_1.endsWith(".") ? attr_1.substring(0, attr_1.length() - 1) : attr_1;
                }
                return attr_1.equals(gear_data.getGear());
            }).mapToLong(gear_data -> gear_data.getGear_count()).sum();
            long total_count = gear_count_list.stream().mapToLong(gear_data -> gear_data.getGear_count()).sum();
            data.setGear_count(gear_count);
            data.setGear_percent(getPercent(getRate(gear_count, total_count)));
            return data;
        }).collect(Collectors.toList());

        return gridOut_list;
    }

}
